package com.joyque.androidbug.report.controller;

import java.util.Enumeration;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.joyque.androidbug.report.component.Pagination;
import com.joyque.androidbug.report.entity.Application;
import com.joyque.androidbug.report.service.ApplicationService;
import com.joyque.androidbug.report.service.BugService;

@Controller
public class AcreBugController {

	@Inject
	private ApplicationService applicationService;
	
	@Inject
	private Pagination page;
	
	@Inject
	private BugService bugService;
	@ResponseBody
	@RequestMapping(value = "/bugs/report", method = RequestMethod.POST)
	public String reportBug(HttpServletRequest request) {
		System.out.println("************************************************************************");
		for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements();) {
			String name = e.nextElement();
			System.out.print(name + ": "); 
			System.out.println(request.getParameter(name));
		}
		System.out.println("************************************************************************");
		applicationService.saveApplication(request.getParameter("PACKAGE_NAME"), request.getParameter("STACK_TRACE"), request.getParameter("DEVICE_ID"), request.getParameter("ANDROID_VERSION"), request.getParameter("APP_VERSION_CODE"), request.getParameter("REPORT_ID"));
		return "OK";
	}
	
	@RequestMapping(value="/bugs", method=RequestMethod.GET)
	public String searchApplication(Model model, HttpServletRequest request){
		List<Application> applicationList = applicationService.findAll();
		page.setTotalCount(35);
		int p = 0;
		String pStr = request.getParameter("p");
		try{
		 if(pStr != null) p = Integer.parseInt(pStr);
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		page.setCurrentPage(p);
		page.setJsFunction("pageClick");
		System.out.println(applicationList.size());
		model.addAttribute("applicationList", applicationList);
		model.addAttribute("page", page);
		return "index";
	}
}
